package com.example.cloudproviderpayment8001.controller;

import com.example.api.entity.CommonResult;
import com.example.api.entity.Payment;
import com.example.cloudproviderpayment8001.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

@RestController
@Slf4j
@RequestMapping("payment")
public class PaymentController {
    @Autowired
    private PaymentService paymentService;

    @Value("${server.port}")
    private String serverPort;

    @PostMapping("/")
    public CommonResult create(@RequestBody Payment payment) {
        int result = paymentService.create(payment);
        log.info("*****插入结果");
        if (result > 0) {
            return new CommonResult<>(200, "插入数据成功", result);
        } else {
            return new CommonResult(444, "插入数据失败");
        }
    }

    @GetMapping("/{id}")
    public CommonResult<Payment> getPaymentById(@PathVariable Long id) {
        Payment result = paymentService.getPaymentById(id);
        if (!ObjectUtils.isEmpty(result)) {
            return new CommonResult<Payment>(200, "获取数据ss成功，端口" + serverPort, result);
        } else {
            return new CommonResult(444, "获取数据失败");
        }
    }

    @GetMapping("/feignTimeout")
    public String feignTimeout() {
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (Exception e) {
        }
        return serverPort;
    }

    @GetMapping("/feignException")
    public String feignException() {
        int a = 10 / 0;
        return serverPort;
    }
}
